package com.ihd.ihardware.view.scooter.view;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.utils.DataUtils;
import com.ihd.ihardware.databinding.ActivityScExamBinding;
import com.ihd.ihardware.pojo.CurrStatus;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.scooter.viewModel.SCViewModel;
import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.wowjoy.commonlibrary.base.BaseActivity;

public class SCExamActivity extends BaseActivity<ActivityScExamBinding, SCViewModel> {
    private DeviceListRes.DataBean.ListBean mBD;
    private CurrStatus mCurrStatus;
    private Observer mObserver;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setLeftImageResource(R.drawable.bike_nb_back);
        binding.mtitlebar.setTitle("故障检测");
        binding.mtitlebar.setTitleColor(getResources().getColor(R.color.C_FFFFFF));

        mBD = (DeviceListRes.DataBean.ListBean) getIntent().getSerializableExtra("sc");

        mObserver = new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                Log.e("onNotify exam", s);
                if (s.startsWith("5A0F03")) {
                    mCurrStatus = DataUtils.parseStatus(s);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.percent.setText("20");
                            binding.e0.setImageResource((mCurrStatus.getErr().equals("霍尔故障") || mCurrStatus.getErr().equals("电机缺相故障")) ? R.drawable.bike_check_wrong : R.drawable.bike_check_right);
                        }
                    }, 500);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.percent.setText("40");
                            binding.e1.setImageResource((mCurrStatus.getErr().equals("控制器故障")) ? R.drawable.bike_check_wrong : R.drawable.bike_check_right);
                        }
                    }, 1000);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.percent.setText("60");
                            binding.e2.setImageResource(mCurrStatus.getErr().equals("转把故障") || mCurrStatus.getErr().equals("断电刹把启动") || mCurrStatus.getErr().equals("其它故障") ? R.drawable.bike_check_wrong : R.drawable.bike_check_right);
                        }
                    }, 1500);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.percent.setText("80");
                            binding.e3.setImageResource((mCurrStatus.getErr().equals("与控制器通讯故障")) ? R.drawable.bike_check_wrong : R.drawable.bike_check_right);
                        }
                    }, 2000);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            binding.percent.setText("100");
                            binding.e4.setImageResource((mCurrStatus.getErr().equals("欠压保护")) ? R.drawable.bike_check_wrong : R.drawable.bike_check_right);
                        }
                    }, 2500);

                    binding.sportview.setProgress(100);
                    LiveEventBus.get()
                            .with("onNotify", String.class).removeObserver(mObserver);
                    binding.modify.setText("检测完成");
                }
            }
        };

        binding.modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.sportview.setProgress(0);
                binding.e0.setImageResource(0);
                binding.e1.setImageResource(0);
                binding.e2.setImageResource(0);
                binding.e3.setImageResource(0);
                binding.e4.setImageResource(0);
                LiveEventBus.get()
                        .with("onNotify", String.class).observeForever(mObserver);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sc_exam;
    }

    @Override
    protected Class getViewModelClass() {
        return SCViewModel.class;
    }
}
